﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS
{
    public partial class addSection : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;
        string currentYear;
        string currentSemester;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Get the type
            currentYear = Request.QueryString["year"];
            currentSemester = Request.QueryString["semester"];
            id = Request.QueryString["cid"];

            Label9.Text = "Current Sections for :" + id;
            Label8.Text = "Current Semester: " + currentSemester + " " + currentYear;

            SqlDataSource1.SelectParameters.Add("sem", currentSemester);
            SqlDataSource1.SelectParameters.Add("y", currentYear);
            SqlDataSource1.SelectParameters.Add("courseID", id);

            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            string secNo = TextBox1.Text;

            if (secNo != "")
            {
                string query2 = "SELECT S.semId " +
                                "FROM Semester S " +
                                "WHERE S.year='" + currentYear + "' AND S.season='" + currentSemester + "'";

                sqlCommand = new MySqlCommand(query2, conn);
                readVals = sqlCommand.ExecuteReader();
                readVals.Read();
                int semID = readVals.GetInt32("semId");
                readVals.Close();

                string query = "INSERT " +
                                "INTO SectionConsists (sectionNo, cid, semId) VALUES(" + secNo + ", '" + id + "', " + semID + ")";
             

               sqlCommand = new MySqlCommand(query, conn);
               sqlCommand.ExecuteNonQuery();

                string url = "addSection.aspx?year=" + currentYear + "&semester=" + currentSemester + "&cid=" + id;             

                Response.Redirect(url);

                
            }
        }

        protected void delete_click(object sender, EventArgs e)
        {
                GridViewRow clickedRow = ((Button)sender).NamingContainer as GridViewRow;

                ClientScript.RegisterStartupScript(this.GetType(), "CloseWin", "<script>window.close()</script>");
                // id yi alıyor cell in 1. indexi
                string key = clickedRow.Cells[1].Text;

                string query = "DELETE " +
                                "FROM SectionConsists " + 
                                "WHERE secCorsId="+ key;
             

               sqlCommand = new MySqlCommand(query, conn);
               sqlCommand.ExecuteNonQuery();

                string url = "addSection.aspx?year=" + currentYear + "&semester=" + currentSemester + "&cid=" + id;             

                Response.Redirect(url);
        }
    }
}